#include <iostream>
#include <ctime>
using namespace std;
class Animal
{
public:
    virtual void speak() = 0;
};

class Dog : public Animal
{
    void speak() override { cout << "我是一只狗，我要叫了：" << "汪\n"; }
};
class Cat : public Animal
{
    void speak() override { cout << "我是一只猫，我要叫了：" << "喵\n"; }
};

class BigObject
{
    char data[1024 * 1024]; // 1MB
};

//  每次传参都会拷贝 1MB 内存
void process1(BigObject obj)
{

    cout << "对象" << endl;
};

//  指针只传 8 字节地址
void process(BigObject *ptr)
{
    cout << "指针" << endl;
};

void process2(BigObject &a)
{
    cout << "引用" << endl;
}

class Player
{
public:
    Animal *Animaler = nullptr;

    void start()
    {

        Animaler = new Dog(); // 按需创建
    }
};

int main()
{
    //  基类指针可以指向任意派生类
    // Animal *animals[2];
    // animals[0] = new Dog();
    // animals[1] = new Cat();

    // for (int i = 0; i < 2; ++i)
    // {
    //     animals[i]->speak();
    // }

    // BigObject *p = nullptr;
    // int s1 = clock();
    // process(p); // 指针
    // int e1 = clock();
    // BigObject b;
    // int s2 = clock();
    // process1(b); // 对象
    // int e2 = clock();
    // int s3 = clock();
    // process2(b);
    // int e3 = clock();
    // cout << "指针耗时：" << e1 - s1 << " 消耗内存: " << sizeof(BigObject *) << endl;
    // cout << "对象耗时：" << e2 - s2 << " 消耗内存: " << sizeof(BigObject) << endl;
    // cout << "引用耗时：" << e3 - s3 << endl;

    Player p;
    //此时没有Animaler指向的对象，也就是没有对象产生
    p.start();//此时才构造处对象
    p.Animaler->speak();
}
